from dataclasses import dataclass
from typing import Any, List
import heapq

@dataclass
class PrioritizedItem:
    priority: int
    count: int  # 用于确保同优先级项目的FIFO顺序
    item: Any

    def __lt__(self, other):
        return (self.priority, self.count) < (other.priority, other.count)

class PriorityQueue:
    def __init__(self):
        self._queue: List[PrioritizedItem] = []
        self._count = 0

    def push(self, item: Any, priority: int = 0) -> None:
        heapq.heappush(self._queue, PrioritizedItem(priority, self._count, item))
        self._count += 1

    def pop(self) -> Any:
        if not self._queue:
            raise IndexError("Queue is empty")
        return heapq.heappop(self._queue).item

    def __len__(self) -> int:
        return len(self._queue)

if __name__ == "__main__":
    pq = PriorityQueue()
    pq.push("item1", 2)
    pq.push("item2", 1)
    pq.push("item3", 3)
    pq.push("item4", 1)
    pq.push("item5", 2)
    pq.push("item6", 9)
    pq.push("item7", 1)
    pq.push("item8", 2)
    for i in range(len(pq)):
        print(pq.pop())